import {
	sub
} from '@/common/event.js'

import store from '@/store/index.js'
import {getRqst} from '@/common/request.js'
import util from "@/utils/index.js"
export default {
	namespaced: true,
	state: {
		list:[],
		current:""
	},
	mutations: {
		setLastTime: (state, payload) => {
			console.log(3344,payload)
			for(let o of state.list){
				if(o.friend_id == payload.id){
					o.read_last_time = payload.nowtime
					o.unread = 0
					break
				}
			}
		},
		setGLastTime: (state, payload) => {
			console.log(33414,payload)
			for(let o of state.list){
				if(o.group_id == payload.id){
					o.read_last_time = payload.nowtime
					o.unread = 0
					break
				}
			}
		},
		initState: (state, payload) => {
			state.list = payload
		},
		updateGLastInfo: (state, payload) => {
			for(let o of state.list){
				console.log(53555,o)
				if(o.group_id == payload.group_id && o.group_id != 0){
					o.content = util.getLastMessageStr(payload)
					o.last_time = payload.create_time
					break
				}
			}
		},
		updateLastInfo: (state, payload) => {
			for(let o of state.list){
				console.log(53555,o)
				if(o.friend_id == payload.friend_id && o.friend_id != 0){
					o.content = util.getLastMessageStr(payload)
					o.last_time = payload.create_time
					break
				}
			}
		},
		onRead:(state, payload) => {
			state.current = `f_${payload}`
		},
		onGRead:(state, payload) => {
			state.current = `g_${payload}`
			// store.commit(`gchat/setCurrentState`,"")
		},
		getGMessage:(state, payload) => {
			if(payload.user_id == store.state.user.user.id){
				store.commit(`chats/updateGLastInfo`,payload)
				return
			}
			if(state.current != `g_${payload.user_id}`){
				for(let o of state.list){
					console.log(53555,o)
					if(o.group_id == payload.group_id && o.group_id != 0){
						o.unread++
						o.content = util.getLastMessageStr(payload)
						o.last_time = payload.create_time
						break
					}
				}
			}
		},
		getFMessage:(state, payload) => {
			if(payload.user_id == store.state.user.user.id){
				store.commit(`chats/updateLastInfo`,payload)
				return
			}
			if(state.current != `f_${payload.user_id}`){
				for(let o of state.list){
					console.log(53555,o)
					if(o.friend_id == payload.user_id  && o.friend_id != 0){
						o.unread++
						o.content = util.getLastMessageStr(payload)
						o.last_time = payload.create_time
						break
					}
				}
			}
		},
	},
	actions: {
		async init({
			commit
		}, {
			payload
		}) {
			const user = uni.getStorageSync('user')
			const ret = await getRqst().get(`chat/chat/chats/${user.id}`)
			commit('initState', ret.data)
			console.log(`join select`,ret)
		},
		async makeGroup({
			commit
		}, {
			payload
		}){
			const ret = await getRqst().post(`chat/chat/makeGroup`,{name:payload.name,list:payload.list})
			console.log(66589,ret)
			store.dispatch(`chats/init`,{})
			payload.callback(ret)
		},
		async updateGReTime({
			commit
		}, {
			payload
		}) {
			const ret = await getRqst().post(`chat/chat/updateGReadTime/${payload}`)
			commit('setGLastTime', ret.data)
		},
		async updateReTime({
			commit
		}, {
			payload
		}) {
			const user_id = store.state.user.user.id
			const ret = await getRqst().post(`chat/chat/updateReadTime/${user_id}/${payload}`)
			commit('setLastTime', ret.data)
		},
	},
	init: async function() {
		const user = uni.getStorageSync('user')
		console.log(`执行chats初始化`,user)
		if(user && user.id){
			store.dispatch(`chats/init`,{})
		}
		sub(`loginSuccess`,function(e){
			store.dispatch(`chats/init`,{})
		})
		sub(`socket_fmessage`,function(e){
			store.commit(`chats/getFMessage`,e)
		})
		sub(`socket_gmessage`,function(e){
			store.commit(`chats/getGMessage`,e)
		})
		sub(`socket_passapply`,function(){
			store.dispatch(`chats/init`,{})
		})
		
	}
}